package com.bishe.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.bishe.entity.User;
import com.bishe.entity.response.LoginInfo;
import com.bishe.entity.response.R;
import com.bishe.entity.response.UserInfo;
import com.bishe.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@RestController
@RequestMapping("user")
@Api(tags = "登录控制器")
public class LoginController {

    @Resource
    private UserService userService;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public R<?> login(@RequestBody LoginInfo loginInfo) {
        UserInfo userInfo = userService.login(loginInfo);
        if (userInfo != null) {
            return R.ok("ok", userInfo);
        }
        return R.error("用户名密码错误!");
    }

    @ApiOperation("退出登录")
    @GetMapping("/loginOut")
    public R<?> loginOut( ){
        StpUtil.logout();
        return R.ok("退出成功!");
    }

    @ApiOperation("检查登录")
    @GetMapping("/isLogin")
    public Boolean isLogin( ){
        return StpUtil.isLogin();
    }

    @ApiOperation("账号注册")
    @PostMapping("/register")
    public R<?> register(@RequestBody User user) {
        return R.ok(userService.save(user));
    }

}
